package com.mew.chess.core.game.dyj.game.task;

import com.mew.chess.core.common.handler.ChessPushMessageUtil;
import com.mew.chess.core.common.handler.GameCommonService;
import com.mew.chess.core.game.dyj.game.DyjGameManager;
import com.mew.chess.core.game.dyj.game.DyjRoomManager;
import com.mew.chess.core.game.dyj.game.common.DyjConstantValue;
import com.mew.chess.core.game.dyj.game.common.DyjLinkType;
import com.mew.chess.core.game.dyj.game.common.DyjPushCMDMapType;
import com.mew.chess.core.game.dyj.game.message.PushDealTimerMessage;
import com.mew.common.enm.UserType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.stream.Collectors;

/**
 * 等待阶段
 * @author dylin
 * @create 2021/4/22
 */
@Component
@Slf4j
public class DyjDealTimerTask {

    @Autowired
    private GameCommonService gameCommonService;

    public boolean dealTimer(int roomId){
        DyjRoomManager roomManager = DyjGameManager.getInstance().getController(roomId);
        roomManager.initInfo();
        roomManager.getOnlineUser().forEach((k, v) -> {
            if (null == v || v.isWeakClose()){
                roomManager.clearOnlineUser(k);
            }
        });

        gameCommonService.checkTMSOnline(roomManager.getOnlineUser().values().stream().collect(Collectors.toList()), DyjGameManager.getInstance().getGameId(),  roomId);

        if (roomManager.getOnlineUser().isEmpty()){
            log.info("Dyj roomId {} not real user is stop ", roomId);
            roomManager.stopGame();
            return false;
        }
//
//        TmsServerInfo tmsServerInfo = TmsConfigInfoManager.getInstance().getServerInfo();
//        if (tmsServerInfo.getOnline() < OnlineType.Online.value()){
//            log.info("Dyj Start TMS not online {}", tmsServerInfo.toString());
//            roomManager.stopGame();
//            roomManager.getOnlineUser().forEach((k, v) -> {
//                aucRedisService.offlineSession(k);
//                ChessPushMessageUtil.pushMessage(v, BasisCommandCode.OVERHAULED.cmd(), new EmptyMessage());
//            });
//        }

        roomManager.changeLink(DyjLinkType.DealTimer.code(), DyjConstantValue.DEAL_TIMER_TIME);
        PushDealTimerMessage message=roomManager.getPushDealTimerMessage();
        message.setIno(roomManager.getIno());
        message.setHistory(roomManager.getHistory());
        message.setBigRList(roomManager.getBigRList().values());
        ChessPushMessageUtil.broadcastMessage(roomManager.getOnlineUser().values(),
                DyjPushCMDMapType.DealTimerMessage.code(), message);
        return true;
    }
}
